package com.example.terminal.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.terminal.R;
import com.example.terminal.adapter.OutboundListDetailsAdapter;
import com.example.terminal.adapter.StoreListDetailsAdapter;
import com.example.terminal.base.BaseActivity;
import com.example.terminal.bean.CommonBean;
import com.example.terminal.bean.OutboudListDetailsBean;
import com.example.terminal.bean.StoreListDetailsBean;
import com.example.terminal.global.Constant;
import com.example.terminal.http.NetworkUtils;
import com.example.terminal.http.network.OkGoBackListener;
import com.example.terminal.listener.OnItemClickListener;
import com.example.terminal.util.ToastUtils;
import com.example.terminal.view.dialog.AlertDialog;
import com.example.terminal.view.edit.EditorActivity;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.io.Serializable;
import java.util.List;

/**
 * 出库办理--详情页面
 */
public class OutboundDetailsActivity extends BaseActivity {

    private RelativeLayout mRlContentNull;
    private TextView mTvTitleName, mTvContent, mTv_OutStore;
    private ImageButton mIbBack;
    private TwinklingRefreshLayout mRefreshLayout;
    private RecyclerView mRvContainer;
    private int mId;
    private String mStoreHouseName;
    private String mOrderName;
    private String mStatus;

    private OutboudListDetailsBean outboudListDetailsBean;
    private List<OutboudListDetailsBean.DataBean.TstoregeOutDeviceListBean> tstoregeOutDeviceList;
    private OutboundListDetailsAdapter outboundListDetailsAdapter;

    @Override
    protected int setContentView() {
        return R.layout.activity_outbound_details;
    }

    @Override
    protected void initView() {
        mRefreshLayout = findViewById(R.id.mRefreshLayout);
        mRvContainer = findViewById(R.id.mRvContainer);
        mRlContentNull = findViewById(R.id.mRlContentNull);

        mTvTitleName = findViewById(R.id.mTvTitleName);//标题
        mIbBack = findViewById(R.id.mIbBack);//返回按键
        mTvContent = findViewById(R.id.mTvContent);//仓库
        mTv_OutStore = findViewById(R.id.mTv_OutStore);//保存按钮

        mTv_OutStore.setOnClickListener(this::onClickSort);
        mIbBack.setOnClickListener(this::onClickSort);
    }

    @Override
    protected void initData() {
        mId = mBundle.getInt(Constant.ID);
        mStoreHouseName = mBundle.getString(Constant.INFO);
        mOrderName = mBundle.getString(Constant.Value);
        mStatus = mBundle.getString(Constant.STATUS);

        mTvTitleName.setText(mOrderName);
        mTvContent.setText(mStoreHouseName);

    }

    @Override
    protected void initConnect() {

        NetworkUtils.getOutboundDetails(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                outboudListDetailsBean = (OutboudListDetailsBean) body;

                tstoregeOutDeviceList = outboudListDetailsBean.getData().getTstoregeOutDeviceList();

                initAdapter();
            }
        }, mId);
    }

    private void initAdapter() {
        if (outboudListDetailsBean == null)
            return;


        mRlContentNull.setVisibility(tstoregeOutDeviceList == null || tstoregeOutDeviceList.size() == 0 ? View.VISIBLE : View.GONE);
        mRefreshLayout.setVisibility(tstoregeOutDeviceList == null || tstoregeOutDeviceList.size() == 0 ? View.GONE : View.VISIBLE);


        if (outboundListDetailsAdapter == null) {
            outboundListDetailsAdapter = new OutboundListDetailsAdapter(mActivity, tstoregeOutDeviceList);
            mRvContainer.setAdapter(outboundListDetailsAdapter);
            mRvContainer.setLayoutManager(new LinearLayoutManager(mActivity));

            outboundListDetailsAdapter.setOnItemClickListener(new DetailsItemClickListener());
        } else {
            outboundListDetailsAdapter.updateAdapter(tstoregeOutDeviceList);
        }
    }

    /**
     * 条目点击事件
     */
    private class DetailsItemClickListener extends OnItemClickListener {
        @Override
        public void onClick(View view, int position) {
            mIntent = new Intent(mActivity, OutboundScanActivity.class);
            mBundle = new Bundle();
            mBundle.putInt(Constant.TurnType, position);
            mBundle.putSerializable(Constant.INFO, outboudListDetailsBean);
            mIntent.putExtras(mBundle);
            turnActForResult(mIntent, 450);
        }
    }


    public void onClickSort(View view) {
        switch (view.getId()) {
            case R.id.mIbBack:
                onFinish();
                break;
            case R.id.mTv_OutStore://保存
                if (!isInFn())
                    return;

                AlertDialog.getInstance(mActivity)
                        .setContent("是否确认办理出库库操作？")
                        .setConfirmButton(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                onSubmits("4");
                            }
                        }).show();
                break;
        }
    }

    private boolean isInFn() {
        if (tstoregeOutDeviceList != null)
            for (int i = 0; i < tstoregeOutDeviceList.size(); i++) {
                int storeDeviceNum = tstoregeOutDeviceList.get(i).getStoreDeviceNum();
                List<OutboudListDetailsBean.DataBean.TstoregeOutDeviceListBean.TstoregeOutDeviceDetailListBean> tstoregeOutDeviceDetailList = tstoregeOutDeviceList.get(i).getTstoregeOutDeviceDetailList();

                if (tstoregeOutDeviceDetailList == null || tstoregeOutDeviceDetailList.size() == 0) {
                    ToastUtils.showToast("设备明细没添加！");
                    return false;
                }

                String imeiStatus = tstoregeOutDeviceList.get(i).getImeiStatus();

                if ("1".equals(imeiStatus)) {
                    if (storeDeviceNum != tstoregeOutDeviceDetailList.size()) {
                        ToastUtils.showToast("设备" + tstoregeOutDeviceList.get(i).getDeviceName() + "的数量不一致!");
                        return false;
                    }
                } else {
                    if (storeDeviceNum != tstoregeOutDeviceDetailList.get(0).getNum()) {
                        ToastUtils.showToast("设备" + tstoregeOutDeviceList.get(i).getDeviceName() + "的数量不一致!");
                        return false;
                    }
                }


            }

        return true;
    }

    private void onSubmits(String status) {
        outboudListDetailsBean.getData().setStatus(status);

        NetworkUtils.getTerminalOut(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                CommonBean commonBean = (CommonBean) body;
                if (commonBean.getCode() == 200) {
                    ToastUtils.showToast(commonBean.getMsg());
                    onFinish();
                }
            }
        }, outboudListDetailsBean.getData());
    }

    // 处理返回结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 450 && resultCode == 550) {
            List<OutboudListDetailsBean.DataBean.TstoregeOutDeviceListBean.TstoregeOutDeviceDetailListBean> serializableExtra = (List<OutboudListDetailsBean.DataBean.TstoregeOutDeviceListBean.TstoregeOutDeviceDetailListBean>) data.getSerializableExtra(Constant.INFO);
            int mPosition = data.getIntExtra(Constant.POSITION, 0);
            int Volume = data.getIntExtra(Constant.Volume, 0);

            tstoregeOutDeviceList.get(mPosition).setStoreDeviceNum(Volume);
            tstoregeOutDeviceList.get(mPosition).setTstoregeOutDeviceDetailList(serializableExtra);

            initAdapter();
        }
    }
}